package com.kuaimacode.kframework.admin.services.user;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.constants.SysAdminUserPasswordConstants;
import com.kuaimacode.kframework.mybatis.mapper.admin.SysAdminMapUserRoleMapper;
import com.kuaimacode.kframework.mybatis.mapper.admin.SysAdminUserMapper;
import com.kuaimacode.kframework.mybatis.models.admin.SysAdminUser;

import java.security.SecureRandom;
import java.util.List;
import java.util.UUID;

import org.apache.commons.codec.binary.Base64;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * 用户信息
 */
@Service
public class  SysAdminUserService extends ServiceImpl<SysAdminUserMapper, SysAdminUser> {

	

	@Autowired
	private SysAdminMapUserRoleMapper sysMapUserRoleMapper;
	
	
	/**
	 * 根据用户名查找用户
	 *
	 * @param userName 用户名
	 * @return
	 */
	public SysAdminUser findByUserName(String userName) {
		return baseMapper.findByUserName(userName);
	}


	/**
	 *  分页查找所有的用户
	 * @param pageNum 当前页码，从1开始
	 * @param pageSize  一页显示多少行
	 * @return
	 */
	public PageInfo<SysAdminUser> findAll(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<SysAdminUser> users = baseMapper.selectList(null);
		return new PageInfo<SysAdminUser>(users);
	}

	/**
	 * 分配角色
	 * @param id 用户id
	 * @param roleIds 角色id
	 */
	public void grant(String id, String[] roleIds) {
		sysMapUserRoleMapper.grant(id, roleIds);
	}

	/**
	 * 修改用户密码
	 *
	 * @param user
	 * @param oldPassword
	 * @param password1
	 * @param password2
	 */
	public void updatePwd(SysAdminUser user, String oldPassword, String password1, String password2) {
		Assert.notNull(user, "用户不能为空");
		Assert.notNull(oldPassword, "原始密码不能为空");
		Assert.notNull(password1, "新密码不能为空");
		Assert.notNull(password2, "重复密码不能为空");

		SysAdminUser dbUser = baseMapper.findByUserName(user.getUserName());
		Assert.notNull(dbUser, "用户不存在");

		String salt = dbUser.getSalt();
		// 旧密码加密后与数据库中的原密码比较是否一致
		String oldPasswordEncrypt = new SimpleHash(SysAdminUserPasswordConstants.HASHALGORITHM_NAME, oldPassword,
				salt, SysAdminUserPasswordConstants.HASHITERATIONS).toString();
		Assert.isTrue(user.getPassword().equals(oldPasswordEncrypt), "原始密码不正确");
		Assert.isTrue(password1.equals(password2), "两次密码不一致");
		String password1Encrypt = new SimpleHash(SysAdminUserPasswordConstants.HASHALGORITHM_NAME, password1,
				salt, SysAdminUserPasswordConstants.HASHITERATIONS).toString();
		dbUser.setPassword(password1Encrypt);
		baseMapper.updateById(dbUser);
	}

	/**
	 * 根据用户id查找roleIds
	 *
	 * @param userId 用户id
	 * @return
	 */
	public List<String> findRoleIdsByUserId(String userId) {
		return sysMapUserRoleMapper.findRoleIdsByUserId(userId);
	}

	/**
	 * 新建保存用户，自动生成salt盐，加密password保存
	 * @param user
	 * @return 
	 */
	public boolean createUser(SysAdminUser user) {
		if (null != user) {
			String password = user.getPassword();
			
			user.setUserId(null);

			// 生成随机salt
			SecureRandom random = new SecureRandom();
			byte bytes[] = new byte[16];
			random.nextBytes(bytes);
			String salt = Base64.encodeBase64String(bytes);
			user.setSalt(salt);

			// 加密密码
			String passwordEncrypt = new SimpleHash(SysAdminUserPasswordConstants.HASHALGORITHM_NAME, password,
					salt, SysAdminUserPasswordConstants.HASHITERATIONS).toString();
			user.setPassword(passwordEncrypt);

			return baseMapper.insert(user) > 0;
		}
		return false;
	}
}

